# -*- coding:utf-8 -*-

from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
import xlrd
from lxml import etree


"""
58同城APP-二手房提取提电话
"""


class Action():
    def __init__(self):
        # 初始化配置，设置Desired Capabilities参数
        self.desired_caps = {
            "platformName": "Android",
            "deviceName": "127.0.0.1:62001",
            "appPackage": "com.wuba",
            "appActivity": "com.wuba.activity.launch.LaunchActivity",
            "udid": "127.0.0.1:62001",
            "noReset": "True",
            "newCommandTimeout": "600",
            # "unicodeKeyboard": True,
            # "resetKeyboard": True

        }
        # 指定Appium Server
        self.server = 'http://localhost:4723/wd/hub'
        # 新建一个Session
        self.driver = webdriver.Remote(self.server, self.desired_caps)
        self.driver.implicitly_wait(10)

        #获取屏幕的高
        self.x = self.driver.get_window_size()['width']
        # 获取屏幕宽
        self.y = self.driver.get_window_size()['height']


    def get_content(self):
        # # 检查搜索框
        # print("---------点击【首页-二手房】------------")
        # self.driver.find_element_by_xpath('//*[contains(@text,"二手房")]').click()
        # print("---------点【二手房】------------")#com.wuba:id/recyclerView
        # WebDriverWait(self.driver, 5).until(
        #     EC.presence_of_element_located((By.ID, "com.wuba:id/iconSdv"))).click()

        self.swipeUp(self.driver)
        while True:
            # 循环列表
            userlist = WebDriverWait(self.driver, 10).until(
                EC.presence_of_all_elements_located((By.ID, 'com.wuba:id/title_text_view')))
            # userlist = self.driver.find_elements_by_xpath('//*[@resource-id="com.wuba:id/title_text_view"]')
            for u in userlist:
                try:
                    dict_data = dict()
                    dict_data['title'] = u.get_attribute("text")
                    print("---------点【列表】------------")
                    u.click()

                    ele = WebDriverWait(self.driver, 10).until(
                            EC.presence_of_element_located((By.ID, 'com.wuba:id/bigTitleTv')))
                    page = self.driver.page_source
                    selector = etree.XML(page.encode('utf-8'))
                    # 标题
                    # dict_data['title1'] = self.driver.find_element_by_id('com.wuba:id/bigTitleTv').get_attribute("text")
                    dict_data['title1'] = ele.text
                    # 总价
                    dict_data['price_count'] = selector.xpath('//android.widget.TextView[@resource-id="com.wuba:id/bigPriceDescTv"]/@text')[0]
                    dict_data['price_count'] += selector.xpath('//android.widget.TextView[@resource-id="com.wuba:id/bigPriceUnitTv"]/@text')[0]
                    # 房型
                    tmp = selector.xpath('//android.widget.TextView[@resource-id="com.wuba:id/bigHouseTypeRoomNumTv"]/@text')[0]
                    tmp += selector.xpath('//android.widget.TextView[@resource-id="com.wuba:id/bigHouseTypeRoomTv"]/@text')[0]
                    tmp += selector.xpath('//android.widget.TextView[@resource-id="com.wuba:id/bigHouseTypeHallNumTv"]/@text')[0]
                    tmp += selector.xpath('//android.widget.TextView[@resource-id="com.wuba:id/bigHouseTypeHallTv"]/@text')[0]
                    tmp += selector.xpath('//android.widget.TextView[@resource-id="com.wuba:id/bigHouseTypeToiletNumTv"]/@text')[0]
                    tmp += selector.xpath('//android.widget.TextView[@resource-id="com.wuba:id/bigHouseTypeToiletTv"]/@text')[0]
                    dict_data['type_room'] = tmp
                    # 面积
                    dict_data['area'] = selector.xpath('//android.widget.TextView[@resource-id="com.wuba:id/bigAreaDescTv"]/@text')[0]
                    dict_data['area'] += selector.xpath('//android.widget.TextView[@resource-id="com.wuba:id/bigAreaUnitTv"]/@text')[0]
                    # 标签
                    dict_data['tag'] = "|".join(selector.xpath('//android.widget.TextView[@resource-id="com.wuba:id/tag_btn"]/@text'))
                    # 单价
                    dict_data['price'] = selector.xpath('//android.widget.TextView[@resource-id="com.wuba:id/priceTv"]/@text')[0]
                    # 楼层
                    dict_data['floor'] = selector.xpath('//android.widget.TextView[@resource-id="com.wuba:id/floorTv"]/@text')[0]
                    # l = self.driver.get_window_size()
                    # x1 = l['width'] * 0.5  # x 坐标
                    # y1 = l['height'] * 0.75  # 起始 y 坐标
                    # y2 = l['height'] * 0.6  # 终点 y 坐标
                    # self.driver.swipe(x1, y1, x1, y2, 1000)
                    # page = self.driver.page_source
                    # selector = etree.XML(page.encode('utf-8'))
                    # 朝向
                    dict_data['orientation'] = selector.xpath('//android.widget.TextView[@resource-id="com.wuba:id/orientationTv"]/@text')[0]
                    # 类型
                    dict_data['type'] = selector.xpath('//android.widget.TextView[@resource-id="com.wuba:id/typeTv"]/@text')[0]
                    # 装修
                    dict_data['decoration'] = selector.xpath('//android.widget.TextView[@resource-id="com.wuba:id/decorationTv"]/@text')[0]
                    # 年代
                    dict_data['age'] = selector.xpath('//android.widget.TextView[@resource-id="com.wuba:id/ageTv"]/@text')[0]
                    # 小区
                    dict_data['area_name'] = selector.xpath('//android.widget.TextView[@resource-id="com.wuba:id/comm_name_tv"]/@text')[0]
                    # 地址
                    dict_data['address'] = selector.xpath('//android.widget.TextView[@resource-id="com.wuba:id/comm_address_tv"]/@text')[0]
                    # 经纪人
                    dict_data['broker'] = "".join(selector.xpath('//android.widget.TextView[@resource-id="com.wuba:id/new_broker_name"]/@text'))
                    dict_data['broker'] += "".join(selector.xpath('//android.widget.TextView[@resource-id="com.wuba:id/broker_name"]/@text'))
                    # 中介
                    dict_data['broker_from'] = "".join(selector.xpath('//android.widget.TextView[@resource-id="com.wuba:id/broker_from"]/@text'))
                    print("---------点击【电话】------------")
                    WebDriverWait(self.driver, 10).until(
                        EC.presence_of_element_located((By.ID, "com.wuba:id/contactbar"))).click()
                    # tel_element = WebDriverWait(self.driver, 10).until(
                    #     EC.presence_of_element_located((By.ID, "com.android.contacts:id/new_contact_name")))
                    # dict_data['tel'] = tel_element.text

                    tel_element = WebDriverWait(self.driver, 10).until(
                            EC.presence_of_element_located((By.ID, "com.cootek.smartdialer:id/phonenumber_input")))
                    dict_data['tel'] = tel_element.text.replace("-", "")
                    print(str(dict_data))
                    self.driver.keyevent(4)  # 返回
                    WebDriverWait(self.driver, 10).until(
                        EC.presence_of_element_located((By.ID, "com.wuba:id/contactbar")))
                    self.driver.keyevent(4)  # 返回
                except:
                    self.driver.keyevent(4)  # 返回
                    pass
            self.swipeUp(self.driver)




    '''
    参数 1：driver
    参数 2：t 是持续时间
    参数 3：滑动次数
    '''
    def swipeUp(self, driver, t=500, n=1):
        '''向上滑动屏幕'''
        l = driver.get_window_size()
        x1 = l['width'] * 0.5  # x 坐标
        y1 = l['height'] * 0.75  # 起始 y 坐标
        y2 = l['height'] * 0.25  # 终点 y 坐标
        for i in range(n):
            driver.swipe(x1, y1, x1, y2, t)

    def swipeDown(self, driver, t=500, n=1):
        '''向下滑动屏幕'''
        l = driver.get_window_size()
        x1 = l['width'] * 0.5  # x 坐标
        y1 = l['height'] * 0.25  # 起始 y 坐标
        y2 = l['height'] * 0.75  # 终点 y 坐标
        for i in range(n):
            driver.swipe(x1, y1, x1, y2, 1)


    def swipLeft(self, driver, t=500, n=1):
        '''向左滑动屏幕'''
        l = driver.get_window_size()
        x1 = l['width'] * 0.75
        y1 = l['height'] * 0.5
        x2 = l['width'] * 0.05
        for i in range(n):
            driver.swipe(x1, y1, x2, y1, t)


    def swipRight(self, driver, t=500, n=1):
        '''向右滑动屏幕'''
        l = driver.get_window_size()
        x1 = l['width'] * 0.05
        y1 = l['height'] * 0.5
        x2 = l['width'] * 0.75
        for i in range(n):
            driver.swipe(x1, y1, x2, y1, t)



if __name__ == '__main__':
    action = Action()
    action.get_content()
